var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
  entry: path.resolve(__dirname, "./demo"),
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: '[name][chunkhash:5].bundle.js',
    chunkFilename: '[name].[chunkhash:5].js',
    publicPath: "/",// dev? "http://localhost:8080/" : "https://cdn/" 可以用来指定输出文件的访问路径/path。 Demo: Dev: "./test.png", prod: "https://cdn/test.png"
  },
  devtool: "cheap-module-source-map",
  devServer: {
    open: true,
    port: 8666,
    hot: true,
    contentBase: path.resolve(__dirname, "./dist")
  },
  plugins: [
    new webpack.DllReferencePlugin({
      manifest: path.resolve(__dirname, 'dist', 'dll', 'manifest.json')
    }),
    new CleanWebpackPlugin({
      cleanOnceBeforeBuildPatterns: ['**/*', '!dll', '!dll/**'] // 不删除dll目录
    }),
    new webpack.HotModuleReplacementPlugin,
    new HtmlWebpackPlugin({
      title: "iWebPack Demo",
      template: path.resolve(__dirname, "./template.html"),
      inject: true,
    }),
    new webpack.ContextReplacementPlugin(
      /moment[/\\]locale$/,
      /zh-cn|en-us/,
    )
  ],
  module: {
    rules: [
      {
        test: /\.js|jsx|ts|ts(x?)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              "@babel/preset-env",
              "@babel/preset-react",
              "@babel/preset-typescript"
            ]
          }
        }
      },
      {
        test: /\.css|scss$/,
        exclude: /mode_modules/,
        use: [
          "style-loader", 'css-loader', 'sass-loader'
        ]
      }, {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: './imgs/[name].[ext]'
            }
          }
        ]
      }
    ]
  },
  optimization: {
    runtimeChunk: {
      name: 'manifest'
    },
    splitChunks: {
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: "all",
          // minChunks: 5,  加此行维护 无法提前vendors文件 ？？？？？？
          priority: 10,
        }
      }
    }
  },
  /**支持webpack, 项目文件内不写扩展名 */
  resolve: {
    extensions: ['.js', '.css', '.scss', '.ts', '.tsx', '.jsx','.png']
  }
}